{{ define "title" }}
{{ .Params.Title | default (T .Data.Plural) | default .Data.Plural | dict "Some" | T "AllSome" }} - {{ .Site.Title }}
{{ end }}
{{ define "main_inner_class" }}{{.Data.Plural}} posts-expand{{ end }}
{{ define "main" }}
<div class="post-block" lang="{{ .Site.LanguageCode }}">
  <div class="post-header">
    <h1>
      {{ if eq .Data.Plural "categories" }}
        {{ T "CateTitle" }}
      {{ else if eq .Data.Plural "tags" }}
        {{ T "TagTitle" }}
      {{ end }}
    </h1>
  </div>
  <div class="post-body">
    {{ if eq .Data.Plural "categories" }}
    <div class="category-all-page">
      <div class="category-all-title">
        {{ T "Terms.categories" (.Scratch.Get "catsCount") | safeHTML }}
      </div>
      <div class="category-all">
        <ul class="category-list">
          {{ $allSecondaryCats := slice }}
          {{ range .Site.Taxonomies.categories.ByCount }}
            {{/*  
              Rollback the change of the primary category name.
              & it could not support the lowercase category name.
              {{ $primaryCategory := .Name }}  
            */}}
            {{ $primaryCategory := .Page.Title }}
            {{ $primaryCount := 0 }}
            {{ $secondaryCategories := slice }}
            {{ $pages := $.Scratch.Get "pages" }}
            {{ range where $pages "Params.categories" "intersect" (slice $primaryCategory) }}
              {{ if eq (index .Params.categories 0) $primaryCategory }}
                {{ $primaryCount = add $primaryCount 1 }}
                {{/** if and (isset .Params.categories 1) (not (in $secondaryCategories (index .Params.categories 1))) **/}}
                {{ if isset .Params.categories 1 }}
                  {{ if not (in $secondaryCategories (index .Params.categories 1)) }}
                    {{ $secondaryCategories = $secondaryCategories | append (index .Params.categories 1) }}
                    {{ $allSecondaryCats = $allSecondaryCats | append (index .Params.categories 1) }}
                  {{ end }}
                {{ end }}
              {{ end }}
            {{ end }}
            
            {{ if not (in $allSecondaryCats $primaryCategory) }}
              <li class="category-list-item">
                <a class="category-list-link" href="{{ "categories/" | relLangURL }}{{ $primaryCategory | urlize }}/">
                  {{ $primaryCategory }}
                  <span class="category-list-count">{{ $primaryCount }}</span>
                </a>
              </li>
              <ul class="category-children-list">
                {{ range $secondaryCategories }}
                  {{ $secondaryCategory := . }}
                  {{ $secondaryCount := 0 }}
                  {{ range where $pages "Params.categories" "intersect" (slice $secondaryCategory) }}
                    {{ if and (eq (index .Params.categories 0) $primaryCategory) (eq (index .Params.categories 1) $secondaryCategory) }}
                      {{ $secondaryCount = add $secondaryCount 1 }}
                    {{ end }}
                  {{ end }}
                  <li class="category-children-list-item">
                    <a class="category-list-link" href="{{ "categories/" | relLangURL }}{{ . | urlize }}/">
                      {{ . }}
                      <span class="category-list-count">{{ $secondaryCount }}</span>
                    </a>
                  </li>
                {{ end }}
              </ul>
            {{ end }}
          {{ end }}
      </ul>
    {{ end }}
    {{ if eq .Data.Plural "tags" }}
      {{ $tags := .Site.Taxonomies.tags }}
      <div class="tag-cloud">
        <div class="tag-cloud-title">
          {{ T "Terms.tags" (.Scratch.Get "tagsCount") | safeHTML }}
        </div>
        <div class="tag-cloud-tags">
          {{ $randNums := (seq 10) }}
          {{- range $name, $items := $tags }}
          <a class="tag-cloud-{{ index (shuffle $randNums) 0 }}" href="{{ "/tags/" | relLangURL }}{{ $name | urlize }}/">{{ .Page.Title }}
            <span class="tag-list-count">
              <sup>({{ len $items }})</sup>
            </span>
          </a>
          {{ end }}
        </div>
      </div>
    {{ end }}
</div>
</div>
{{ end }}
